package com.cjm.one.gateway.filter;

import com.cjm.one.common.constant.SecurityConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;


@Slf4j
@Component
public class GatewayGlableFillter implements GlobalFilter , Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        //过滤请求头中的允许内部访问的参数
        ServerHttpRequest request = exchange.getRequest().mutate()
        .headers(
                httpHeaders -> httpHeaders.remove(SecurityConstants.FROM_SOURCE)).build();
        // 获取请求路径
//        .header(HttpHeaders.CONTENT_TYPE, "multipart/form-data") // 强制设置头
        String path = request.getURI().getPath();
        HttpMethod method = request.getMethod();
        log.info("请求方式: {}", method);
        log.info("请求路径: {}", path);
//        HttpHeaders headers = request.getHeaders();
//        log.info("headers:{}", headers);
//        List<String> token = headers.get("token");
//        log.info("token1:{}", token.get(0));
        return chain.filter(exchange.mutate().request(request).build());
    }


    @Override
    public int getOrder() {
        return 0;
    }
}
